package com.clean.scanlibrary.img.adapter

import android.app.Activity
import android.content.Context
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import bd.FB
import oa.lla.cca.R
import oa.lla.cca.databinding.ItemImgSelectLayoutBinding
import bd.CH
import bd.CJ


class PicSelectAdapter(val imageList: List<FB>, val context: Context) :
    RecyclerView.Adapter<PicSelectAdapter.Holder>() {

    private var selectParams = ArrayList<FB>()
    private var onItemSelectClickListener: OnItemSelectClickListener? = null
    private val maxSelectImage = 6

    class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val binding: ItemImgSelectLayoutBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.item_img_select_layout,
            parent,
            false
        )
        return Holder(binding.root)
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val bind = DataBindingUtil.getBinding<ItemImgSelectLayoutBinding>(holder.itemView)

        bind?.apply {
            imageList[position].apply {
                selectStatusView.visibility = View.GONE

                itemImageSize.text = formatSize("$fileSize")
                setImageSourceText(fromSource, path)
                if (isSelect) {
                    selectStatusView.setImageResource(R.drawable.select_true_icon)
                } else {
                    selectStatusView.setImageResource(R.drawable.select_default_icon)
                }

                //选中操作
                selectStatusView.setOnClickListener {
                    if (!isSelect && selectParams.size >= maxSelectImage) {
                        Toast.makeText(context, "单词操作只支持${maxSelectImage}张图", Toast.LENGTH_SHORT)
                            .show()
                        return@setOnClickListener
                    }
                    if (isSelect) {
                        selectStatusView.setImageResource(R.drawable.select_default_icon)
                    } else {
                        selectStatusView.setImageResource(R.drawable.select_true_icon)
                    }
                    isSelect = !isSelect

                    if (isSelect) {
                        if (!selectParams.contains(imageList[position])) {
                            selectParams.add(imageList[position])
                        }
                    } else {
                        if (selectParams.contains(imageList[position])) {
                            selectParams.remove(imageList[position])
                        }
                    }
                    onItemSelectClickListener?.selectIndex(selectParams)
                }

                itemImgLayout.setOnClickListener {

                    if (isFastClick()){
                        return@setOnClickListener
                    }

                    if (pathType==0){
                         CH.intentImgDetailsActivity(
                            context = context as Activity,
                            imageList[position],
                            position
                        )

//                        PicStyleActivity.intentPicStyleActivity(context,imageList[position].path)



                    }else{
                        CJ.intentVideoDetailsActivity(
                            context = context as Activity,
                            imageList[position],
                            position
                        )
                    }


                }


                if (pathType == 0) {
                    setImageGlide(path, bind.itemImg)
                    fromSource.visibility = View.VISIBLE
                } else {
                    fromSource.visibility = View.GONE
                    Glide.with(context)
                        .setDefaultRequestOptions(
                            RequestOptions()
                                .frame(0)
                                .centerCrop()
                        )
                        .load(path)
                        .into(bind.itemImg)
                }

            }

        }
    }


    private fun setImageSourceText(text: TextView, path: String) {
        if (path.contains("微信")) {
            text.text = "微信"
        } else if (path.contains("Camera") || path.contains("Pictures")) {
            text.text = "相册"
        } else if (path.contains("cache")) {
            text.text = "缓存"
        } else if (path.contains("emoji")) {
            text.text = "表情"
        } else if (path.contains("weibo")) {
            text.text = "微博"
        } else {
            text.text = "未知"
        }


    }


    private fun formatSize(target_size: String): String? {
        return Formatter.formatFileSize(context, java.lang.Long.valueOf(target_size))
    }


    private fun setImageGlide(path: String, img: ImageView) {
        val roundedCorners = RoundedCorners(10)
        val options = RequestOptions.bitmapTransform(roundedCorners)
        Glide.with(context)
            .load(path)
            .error(R.drawable.default_icon)
            .apply(options)
            .into(img)
    }


    interface OnItemSelectClickListener {
        fun selectIndex(bean: ArrayList<FB>)
    }


    fun setOnItemSelectClickListener(onItemSelectClickListener: OnItemSelectClickListener) {
        this.onItemSelectClickListener = onItemSelectClickListener
    }


    override fun getItemCount(): Int = imageList.size




    // 两次点击间隔不能少于1000ms
    private val FAST_CLICK_DELAY_TIME = 1000;
    private var lastClickTime: Long = 0
    fun isFastClick(): Boolean {
        var flag: Boolean = true;
        val currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= FAST_CLICK_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;
    }
}